package view;

import dao.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import commen.*;
import dao.*;

@WebServlet("/delete")
public class blog_delete extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //1、先获取到用户的登录状态
        User user= check.checkSession(req);
        if(user==null){
            String html="<h3>您尚未登录，不能删除</h3>";
            resp.getWriter().write(html);
            return;
        }
        //2.获取到当前请求中要删除的博客id
        String blogId=req.getParameter("blogId");
        if(blogId==null||blogId.equals("")){
            String html="<h3>blogId参数缺失";
            resp.getWriter().write(html);
        }
        //3.校验一下当前执行删除的是否为作者
        blog blog= blogdao.getBlog(Integer.parseInt(blogId));
        if(blog.getUseId()!= user.getUserId()){
            String html="<h3>您不是博客的作者，不能删除</h3>";
            resp.getWriter().write(html);
            return;
        }
        //4.删除
        blogdao.delete(Integer.parseInt(blogId));
        //5.重定向
        System.out.println(123);
        resp.sendRedirect("blog_list.html");
    }
}
